#include "LevelSelectScene.h"

#include "VisibleRect.h"
#include "RectLayout.h"

#include "TitleScene.h"
#include "HelloWorldScene.h"
#include "LevelSelectTableLayer.h"

CCScene* LevelSelectScene::scene(){
	CCScene * scene = NULL;
	do 
	{
		// 'scene' is an autorelease object
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		// 'layer' is an autorelease object
		LevelSelectScene *LevelSelectLayer = LevelSelectScene::create();
		CC_BREAK_IF(! LevelSelectLayer);

		// add layer as a child to scene
		scene->addChild(LevelSelectLayer);

		LevelSelectTableLayer *levelSelectTableLayer = LevelSelectTableLayer::create();
		CC_BREAK_IF(!levelSelectTableLayer);

		scene->addChild(levelSelectTableLayer);

	} while (0);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool LevelSelectScene::init()
{
	bool bRet = false;
	do 
	{
		//////////////////////////////////////////////////////////////////////////
		// super init first
		//////////////////////////////////////////////////////////////////////////

		CC_BREAK_IF(! CCLayer::init());

		CCSize size = CCDirector::sharedDirector()->getWinSize();

		CCRect r(0, 0, size.width, size.height);
		CCSprite* pSpriteTopMiddle = CCSprite::create("images/interface/dialog_centermiddle_edit.png",r);
		CC_BREAK_IF(! pSpriteTopMiddle);
		// ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT,GL_REPEAT};
		ccTexParams tp = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT };  
		pSpriteTopMiddle->getTexture()->setTexParameters(&tp);

		RectLayout::AlignToCenter(pSpriteTopMiddle);
		this->addChild(pSpriteTopMiddle, 0);

		CCRect rLeft(0, 0, 50, size.height);
		CCSprite* pSpriteCenterLeft = CCSprite::create("images/interface/dialog_centerleft_edit2.png",rLeft);
		CC_BREAK_IF(! pSpriteCenterLeft);

		ccTexParams ccTexParamsLeft = {GL_LINEAR, NULL, NULL, NULL};
		pSpriteCenterLeft->getTexture()->setTexParameters(&ccTexParamsLeft);

		RectLayout::AlignToLeft(pSpriteCenterLeft);
		this->addChild(pSpriteCenterLeft, 0);

		CCRect rBottom(0, 50, size.width, 50);
		CCSprite* pSpriteCenterBottom = CCSprite::create("images/interface/dialog_bottommiddle_edit.png",rBottom);
		CC_BREAK_IF(! pSpriteCenterBottom);

		ccTexParams ccTexParamsBottom = {GL_LINEAR, NULL, NULL, NULL};
		pSpriteCenterBottom->getTexture()->setTexParameters(&ccTexParamsBottom);

		RectLayout::AlignToBottom(pSpriteCenterBottom);
		this->addChild(pSpriteCenterBottom, 0);


		CCSprite* pSpriteTopLeft = CCSprite::create("images/interface/dialog_topleft.png");
		CC_BREAK_IF(!pSpriteTopLeft);

		RectLayout::AlignToTopLeft(pSpriteTopLeft);
		this->addChild(pSpriteTopLeft, 0);

		CCSprite* pSpriteTopRight = CCSprite::create("images/interface/dialog_topright.png");
		CC_BREAK_IF(! pSpriteTopRight);
		RectLayout::AlignToTopRight(pSpriteTopRight);
		this->addChild(pSpriteTopRight, 0);

		CCSprite* pSpriteBottomLeft = CCSprite::create("images/interface/dialog_bottomleft.png");
		CC_BREAK_IF(! pSpriteBottomLeft);

		RectLayout::AlignToBottomLeft(pSpriteBottomLeft);
		this->addChild(pSpriteBottomLeft, 0);

		CCSprite* pSpriteBottomRight = CCSprite::create("images/interface/dialog_bottomright.png");
		CC_BREAK_IF(! pSpriteBottomRight);

		RectLayout::AlignToBottomRight(pSpriteBottomRight);
		this->addChild(pSpriteBottomRight, 0);

		bRet = true;
	} while (0);

	return bRet;
}

void LevelSelectScene::menuCloseCallback(CCObject* pSender)
{
	// "close" menu item clicked
	CCDirector::sharedDirector()->end();
}

void LevelSelectScene::menuStartCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::create(0.2f, TitleScene::scene()));
}